package core.view
{
	import com.greensock.TweenLite;
	
	import core.controller.CopyFile;
	import core.event.CopyFileEvent;
	import core.model.ConfigData;
	import core.view.dirlist.DirListView;
	
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragManager;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.text.TextField;
	
	import org.ais.event.TEvent;
	import org.ais.system.Ais;
	import org.aisy.component.TextView;
	import org.aisy.display.USprite;
	import org.aisy.scroller.Scroller;
	import org.aisy.skin.AisySkin;
	import org.aisy.upwindow.UpWindowAis;

	public class CopyFileView extends USprite
	{
		static protected var instance:CopyFileView;
		protected var _copyFile:CopyFile;
		protected var _uMask:USprite;
		protected var _group:USprite;
		protected var _scroller:Scroller;
		
		public function CopyFileView()
		{
			init();
		}
		
		protected function init():void
		{
			new UpWindowAis();
//			AisySkin.MOBILE = true;
			
			_copyFile = new CopyFile();
			
			TEvent.newTrigger(CopyFileEvent.COPY_FILE_EVENT, __triggerHandler);
			
			initGroup();
			
			addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, __nativeDragHandler);
			addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT, __nativeDragHandler);
			addEventListener(NativeDragEvent.NATIVE_DRAG_OVER, __nativeDragHandler);
			addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, __nativeDragHandler);
			
			Ais.IMain.stage.addEventListener(Event.RESIZE, __stageHandler);
			Ais.IMain.stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		public function initGroup():void
		{
			if (null !== _group) _group.clear();
			_group = new USprite();
			
			var tv:TextView = new TextView();
			tv.name = "edit";
			tv.setText("Edit");
			tv.buttonMode = true;
			tv.addEventListener(MouseEvent.CLICK, __textViewHandler);
			_group.addChild(tv);
			
			if (null !== ConfigData.configData && ConfigData.configData.hasOwnProperty("list") === true) {
				var dirListView:DirListView = new DirListView();
				dirListView.y = _group.height + 7;
				_group.addChild(dirListView);
			}
			
			addChild(_group);
		}
		
		public function copyFiles(fileArr:Array, parentFile:File = null):void
		{
			_copyFile.copyFiles(fileArr, parentFile, showMessage);
		}
		
		protected function __getMessage(arr:Array, file:File, indent:String = ""):String
		{
			var str:String = "";
			if (undefined !== arr[3]) {
				for each (var a:Array in arr[3]) {
					str += __getMessage(a[0], a[1], indent + "    ");
				}
			}
			if (undefined !== arr[0]) {
				for each (var f:File in arr[0]) {
					str += indent + f.name + "\r";
				}
			}
			return str ? ((null !== file) ? "~" + file.name + "/\r" : "") + str : "";
		}
		
		protected function showMessage(arr:Array):void
		{
			if (null !== _scroller) {
				_scroller.clear();
				_scroller = null;
			}
			if (null === arr) return;
			var str:String = "";
			for each (var i:Array in arr) {
				str += "*********\r" + __getMessage(i[0], i[1]);
			}
			var textField:TextField = new TextField();
			textField.defaultTextFormat = AisySkin.TEXTFORMAT();
			textField.autoSize = "left";
			textField.wordWrap = true;
			textField.selectable = false;
			textField.width = Ais.IMain.stage.stageWidth;
			textField.htmlText = str;
			
			var us:USprite = new USprite();
			us.addChild(textField);
			us.graphics.beginFill(0x7F7F7F);
			us.graphics.drawRect(0, 0, us.width, us.height);
			us.graphics.endFill();
			
			_scroller = new Scroller();
			_scroller.setSource(us);
			_scroller.setMoveFactor(3.8);
			_scroller.setDelta(20);
			_scroller.y = _group.height + 7;
			_scroller.setSize(Ais.IMain.stage.stageWidth, Ais.IMain.stage.stageHeight - _group.height - 7);
			_group.addChild(_scroller);
		}
		
		protected function showMask():void
		{
			if (null !== _uMask) return;
			_uMask = new USprite();
			_uMask.graphics.beginFill(0x000000, 0.5);
			_uMask.graphics.drawRect(0, 0, Ais.IMain.stage.stageWidth, Ais.IMain.stage.stageHeight);
			_uMask.graphics.endFill();
			addChild(_uMask);
		}
		
		protected function clearMask():void
		{
			if (null === _uMask) return;
			_uMask.clear();
			_uMask = null;
		}
		
		protected function __textViewHandler(e:MouseEvent):void
		{
			switch (e.type) {
				case MouseEvent.CLICK:
					switch (e.currentTarget.name) {
						case "edit":
							ConfigEditView.getInstance();
							break;
					}
					break;
			}
		}
		
		protected function __nativeDragHandler(e:NativeDragEvent):void
		{
			switch (e.type) {
				case NativeDragEvent.NATIVE_DRAG_OVER:
					showMask();
					break;
				case NativeDragEvent.NATIVE_DRAG_ENTER:
					NativeDragManager.acceptDragDrop(this);
					showMask();
					break;
				case NativeDragEvent.NATIVE_DRAG_EXIT:
					clearMask();
					break;
				case NativeDragEvent.NATIVE_DRAG_DROP:
					clearMask();
					copyFiles(e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array);
					break;
			}
		}
		
		protected function __stageHandler(e:Event):void
		{
			this.graphics.clear();
			this.graphics.beginFill(0xAAAAAA);
			this.graphics.drawRect(0, 0, Ais.IMain.stage.stageWidth, Ais.IMain.stage.stageHeight);
			this.graphics.endFill();
		}
		
		protected function __triggerHandler(type:String, data:* = null):void
		{
			switch (type) {
				case CopyFileEvent._CHANGE:
					if (data.hasOwnProperty("title") === true) Ais.IMain.stage.nativeWindow.title = data.title;
					_copyFile.setData(data);
					break;
			}
			type = null;
			data = null;
		}
		
		private function __import():void
		{
			TweenLite;
			
			_AISY_SCROLLER_SKIN;
		}
		
		override public function clear():void
		{
			Ais.IMain.stage.removeEventListener(Event.RESIZE, __stageHandler);
			TEvent.clearTrigger(CopyFileEvent.COPY_FILE_EVENT);
			if (null !== _copyFile) _copyFile.clear();
			_copyFile = null;
			_uMask = null;
			_scroller = null;
			instance = null;
			super.clear();
		}
		
		static public function getInstance():CopyFileView
		{
			if (null === instance) instance = new CopyFileView();
			return instance;
		}
		
	}
}